// +----------------------------------------------------------------------
// | 网评管理系统V4.0
// +----------------------------------------------------------------------
// | 版权所有 2006~2021 广西龙讯互动信息技术股份有限公司
// +----------------------------------------------------------------------
// | 官方网站: http://www.longsunhd.com/
// +----------------------------------------------------------------------
// | 作者: 鲲鹏 <414797210@qq.com>
// +----------------------------------------------------------------------

package com.javaweb.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.javaweb.common.config.CommonConfig;
import com.javaweb.common.utils.JsonResult;
import com.javaweb.common.utils.StringUtils;
import com.javaweb.common.common.BaseQuery;
import com.javaweb.common.common.BaseServiceImpl;
import com.javaweb.system.dto.GroupPermissionDto;
import com.javaweb.system.entity.GroupMenu;
import com.javaweb.system.entity.MemberGroup;
import com.javaweb.system.entity.MemberMenu;
import com.javaweb.system.mapper.GroupMenuMapper;
import com.javaweb.system.mapper.MemberGroupMapper;
import com.javaweb.system.query.MemberGroupQuery;
import com.javaweb.system.service.IGroupMenuService;
import com.javaweb.system.service.IMemberGroupService;
import com.javaweb.system.service.IMemberMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 会员级别表 服务实现类
 * </p>
 *
 * @author 鲲鹏
 * @since 2020-11-04
 */
@Service
public class MemberGroupServiceImpl extends BaseServiceImpl<MemberGroupMapper, MemberGroup> implements IMemberGroupService {

    @Autowired
    private MemberGroupMapper memberGroupMapper;
    @Autowired
    private IMemberMenuService memberMenuService;
    @Autowired
    private GroupMenuMapper groupMenuMapper;
    @Autowired
    private IGroupMenuService groupMenuService;

    /**
     * 获取会员分组列表
     *
     * @param query 查询条件
     * @return
     */
    @Override
    public JsonResult getList(BaseQuery query) {
        MemberGroupQuery memberGroupQuery = (MemberGroupQuery) query;
        // 查询条件
        QueryWrapper<MemberGroup> queryWrapper = new QueryWrapper<>();
        // 会员分组名称
        if (!StringUtils.isEmpty(memberGroupQuery.getName())) {
            queryWrapper.like("name", memberGroupQuery.getName());
        }
        queryWrapper.eq("mark", 1);
        queryWrapper.orderByAsc("sort");

        // 查询分页数据
        IPage<MemberGroup> page = new Page<>(memberGroupQuery.getPage(), memberGroupQuery.getLimit());
        IPage<MemberGroup> pageData = memberGroupMapper.selectPage(page, queryWrapper);
        return JsonResult.success(pageData);
    }

    /**
     * 获取会员分组列表
     *
     * @return
     */
    @Override
    public JsonResult getMemberGroupList() {
        QueryWrapper<MemberGroup> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("mark", 1);
        queryWrapper.orderByAsc("sort");
        return JsonResult.success(list(queryWrapper));
    }

    @Override
    public JsonResult getMenuList(Integer groupId) {
        List<MemberMenu> menuList = memberMenuService.getMemberMenuAll();
        if (!menuList.isEmpty()) {
            // 获取角色菜单列表
            QueryWrapper<GroupMenu> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("group_id", groupId);
            queryWrapper.select("menu_id");
            List<GroupMenu> roleMenuList = groupMenuMapper.selectList(queryWrapper);
            List<Integer> menuIds = roleMenuList.stream().map(p -> p.getMenuId()).collect(Collectors.toList());
            menuList.forEach(item -> {
                if (menuIds.contains(item.getId())) {
                    item.setChecked(true);
                    item.setOpen(true);
                }
            });
        }
        return JsonResult.success(menuList);
    }

    @Override
    public JsonResult savePermission(GroupPermissionDto groupPermissionDto) {
        if (CommonConfig.appDebug) {
            return JsonResult.error("演示环境禁止操作");
        }
        // 分组ID校验
        if (StringUtils.isNull(groupPermissionDto.getGroupId())) {
            return JsonResult.error("分组ID不能为空");
        }
        // 同步删除分组菜单关系数据
        groupMenuService.deleteGroupMenus(groupPermissionDto.getGroupId());
        // 插入新的角色菜单关系数据
        List<GroupMenu> groupMenuList = new ArrayList<>();
        if (!StringUtils.isEmpty(groupPermissionDto.getMenuIds())) {
            for (String menuId : groupPermissionDto.getMenuIds()) {
                GroupMenu groupMenu = new GroupMenu();
                groupMenu.setGroupId(groupPermissionDto.getGroupId());
                groupMenu.setMenuId(Integer.valueOf(menuId));
                groupMenuList.add(groupMenu);
            }
        }
        // 批量插入角色菜单关系数据
        groupMenuService.saveBatch(groupMenuList);
        return JsonResult.success("权限保存成功");
    }
}
